package com.now.printer.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.utils.ConvertUtils;
import com.now.printer.utils.PrinterUtils;

import org.xutils.x;

import cn.jarlen.photoedit.crop.CropImageType;
import cn.jarlen.photoedit.crop.CropImageView;
import cn.jarlen.photoedit.photoframe.PhotoFrame;
import cn.jarlen.photoedit.utils.FileUtils;

/**
 * Created by xudong on 2021/6/12.
 */

public class TuPianActivity extends BaseActivity2 implements View.OnClickListener {

    public final static String INTENT_KEY_FILE_URI = "INTENT_KEY_FILE_URI";
    public final static String INTENT_KEY_FILE_NAME = "INTENT_KEY_FILE_NAME";
    public final static String INTENT_KEY_FILE_IMAGE = "INTENT_KEY_FILE_IMAGE";
    public final static String INTENT_KEY_FILE_LAST_TIME = "INTENT_KEY_FILE_LAST_TIME";
    public final static String INTENT_KEY_FILE_SIZE = "INTENT_KEY_FILE_SIZE";

    private String fileUri = "", fileLastTime, fileSize, fileName;
    private int fileImage;
    private RelativeLayout rlTop;
    private ImageView imgToBack;
    private TextView imgHelp;
    private TextView tvTitle;
    private CropImageView cropImage;

    private TextView tvXuanzhuan;
    private TextView tvSuofang;
    private TextView tvBiankuang;

    private HorizontalScrollView photoResList;
    private LinearLayout photoRes;
    private ImageView photoResOne;
    private ImageView photoResTwo;
    private ImageView photoResThree;
    private PhotoFrame mImageFrame;

    //    private Bitmap mBitmap;
    private Bitmap mTmpBmp;

    @Override
    public int getLayoutId() {
        return R.layout.activity_tupian;
    }

    @Override
    public void doBusiness(Context mContext) {

        fileUri = getIntent().getStringExtra(INTENT_KEY_FILE_URI);
        fileName = getIntent().getStringExtra(INTENT_KEY_FILE_NAME);
        fileImage = getIntent().getIntExtra(INTENT_KEY_FILE_IMAGE, -1);
        fileLastTime = getIntent().getStringExtra(INTENT_KEY_FILE_LAST_TIME);
        fileSize = getIntent().getStringExtra(INTENT_KEY_FILE_SIZE);


        rlTop = (RelativeLayout) findViewById(R.id.rl_top);
        imgToBack = (ImageView) findViewById(R.id.img_toBack);
        imgHelp = (TextView) findViewById(R.id.imgHelp);
        imgHelp.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        cropImage = (CropImageView) findViewById(R.id.cropmageView1);

        tvXuanzhuan = (TextView) findViewById(R.id.tv_xuanzhuan);
        tvSuofang = (TextView) findViewById(R.id.tv_suofang);
        tvBiankuang = (TextView) findViewById(R.id.tv_biankuang);


        photoResList = (HorizontalScrollView) findViewById(R.id.photoResList);
        photoRes = (LinearLayout) findViewById(R.id.photoRes);
        photoResOne = (ImageView) findViewById(R.id.photoRes_one);
        photoResTwo = (ImageView) findViewById(R.id.photoRes_two);
        photoResThree = (ImageView) findViewById(R.id.photoRes_three);


        Bitmap bit = BitmapFactory.decodeFile(fileUri);
        mTmpBmp = bit;

        Bitmap hh = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.crop_button);
        cropImage.setCropOverlayCornerBitmap(hh);
        cropImage.setImageBitmap(bit);

        // Bitmap bit =
        // BitmapFactory.decodeResource(this.getResources(),R.drawable.hi0);

//        cropImage.setGuidelines(CropImageType.CROPIMAGE_GRID_ON_TOUCH);// 触摸时显示网格

        cropImage.setGuidelines(CropImageType.CROPIMAGE_GRID_ON);// 图片剪切网格显示开启

        cropImage.setFixedAspectRatio(false);// 自由剪切
//        cropImage.setCropWindow(413/2,295/2);

        tvXuanzhuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photoResList.setVisibility(View.GONE);
                cropImage.rotateImage(90);
            }
        });

        tvSuofang.setOnClickListener(this);

        tvBiankuang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cropImage.getCropOverlayView().setVisibility(View.GONE);
                photoResList.setVisibility(View.VISIBLE);
            }
        });

        findViewById(R.id.photoRes_one).setOnClickListener(
                new PhotoFrameOnClickListener());
        findViewById(R.id.photoRes_two).setOnClickListener(
                new PhotoFrameOnClickListener());
        findViewById(R.id.photoRes_three).setOnClickListener(
                new PhotoFrameOnClickListener());

        reset();
        mImageFrame = new PhotoFrame(this, bit);
    }


    /**
     * 重新设置一下图片
     */
    private void reset() {
        if (cropImage != null) {
            cropImage.setImageBitmap(mTmpBmp);
            cropImage.invalidate();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgHelp:

                x.task().run(new Runnable() {
                    @Override
                    public void run() {
                        String saveFileUri = FileUtils.saveBitmapToCamera(TuPianActivity.this, mTmpBmp, "");
                        Message message = new Message();
                        message.what = 1;
                        message.obj = saveFileUri;
                        handler.sendMessage(message);
                    }
                });

                break;
            case R.id.tv_suofang:
                 photoResList.setVisibility(View.GONE);
                cropImage.rotateImage(90);
                break;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Intent intent = new Intent();
                intent.setClass(TuPianActivity.this, FilePreviewAndPrintActivity.class);
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_FILE_URI, (String)msg.obj);
                startActivity(intent);
            }
        }
    };


    private class PhotoFrameOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {

                case R.id.photoRes_one:

                    mImageFrame.setFrameType(PhotoFrame.FRAME_SMALL);
                    mImageFrame.setFrameResources(
                            R.drawable.frame_around1_left_top,
                            R.drawable.frame_around1_left,
                            R.drawable.frame_around1_left_bottom,
                            R.drawable.frame_around1_bottom,
                            R.drawable.frame_around1_right_bottom,
                            R.drawable.frame_around1_right,
                            R.drawable.frame_around1_right_top,
                            R.drawable.frame_around1_top);
                    mTmpBmp = mImageFrame.combineFrameRes();

                    break;

                case R.id.photoRes_two:

                    mImageFrame.setFrameType(PhotoFrame.FRAME_SMALL);
                    mImageFrame.setFrameResources(
                            R.drawable.frame_around2_left_top,
                            R.drawable.frame_around2_left,
                            R.drawable.frame_around2_left_bottom,
                            R.drawable.frame_around2_bottom,
                            R.drawable.frame_around2_right_bottom,
                            R.drawable.frame_around2_right,
                            R.drawable.frame_around2_right_top,
                            R.drawable.frame_around2_top);
                    mTmpBmp = mImageFrame.combineFrameRes();

                    break;

                case R.id.photoRes_three:
                    mImageFrame.setFrameType(PhotoFrame.FRAME_BIG);
                    mImageFrame.setFrameResources(R.drawable.frame_big1);

                    mTmpBmp = mImageFrame.combineFrameRes();

                    break;

                default:
                    break;

            }
            reset();

        }

    }
}
